/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.game;

import com.jogamp.opengl.util.FPSAnimator;
import deatax.game.state.StateManager;
import deatax.game.state.TestGameState;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;

/**
 *
 * @author pson
 */
public class GameManager implements GLEventListener {

    private JFrame frame;
    private GLCanvas canvas;
    private FPSAnimator animator;
    private GameData data;
    private StateManager state;
    private long time;

    public GameManager() {


        state = new StateManager();
        state.addState(new TestGameState());
        state.pushState("test");


        GLCapabilities glc = new GLCapabilities(null);

        glc.setDoubleBuffered(true);
        canvas = new GLCanvas(glc);
        canvas.addGLEventListener(this);
        animator = new FPSAnimator(canvas, 60);

        frame = new JFrame("PieTax");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1024, 768);
        frame.setLocationRelativeTo(null);

        frame.getContentPane().add(canvas);
        frame.setVisible(true);

        time = System.currentTimeMillis();
        animator.start();
    }

    @Override
    public void init(GLAutoDrawable drawable) {

        GL2 gl = drawable.getGL().getGL2();
        //GLU glu = GLU.createGLU();

        gl.glEnable(GL.GL_BLEND);
        gl.glEnable(GL.GL_LINE_SMOOTH);
        gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST);
        gl.glHint(GL2.GL_POINT_SMOOTH_HINT, GL2.GL_NICEST);

        gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glEnable(GL2.GL_DEPTH_BUFFER);
        
        state.init(gl);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        long p_time = time;
        time = System.currentTimeMillis();
        long delta = time - p_time;
        state.update(delta);
        state.draw(gl, delta);
        canvas.swapBuffers();
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL2 gl = drawable.getGL().getGL2();
        GLU glu = GLU.createGLU();

        h = (h == 0) ? 1 : h;  // Prevent divide by zero
        float aspect = (float) w / h; // Compute aspect ratio

        // Set view port to cover full screen
        gl.glViewport(0, 0, w, h);

        // Set up the projection matrix - choose perspective view
        gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
        gl.glLoadIdentity();    // reset
        // Angle of view (fovy) is 45 degrees (in the up y-direction). Based on this
        // canvas's aspect ratio. Clipping z-near is 0.1f and z-near is 100.0f.
        glu.gluPerspective(45.0f, aspect, 0.1f, 500.0f); // fovy, aspect, zNear, zFar
//      gl.glRotated
        // Switch to the model-view transform
        gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
        gl.glLoadIdentity();    // reset
    }
}
